package xin.marcher.module.rights.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.rights.api.PermissionApi;
import xin.marcher.module.rights.domain.dto.PermissionDTO;
import xin.marcher.module.rights.domain.request.PermissionRequest;
import xin.marcher.module.rights.domain.request.QueryPermissionRequest;
import xin.marcher.module.rights.domain.request.RemovePermissionRequest;
import xin.marcher.module.rights.service.PermissionService;

import java.util.List;

/**
 *
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = PermissionApi.class, retries = 0)
public class PermissionApiImpl implements PermissionApi {

    @Autowired
    private PermissionService permissionService;

    @Override
    public JsonResult savePermission(PermissionRequest request) {
        try {
            return permissionService.savePermission(request);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult removePermission(RemovePermissionRequest request) {
        try {
            return permissionService.removePermission(request);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult queryPermission(QueryPermissionRequest request) {
        try {
            List<PermissionDTO> permissionDTOS = permissionService.queryPermission(request);
            return JsonResult.buildSuccess(permissionDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
